08. util.h
C02 L06 A02 Class Util Example Part 1
C02 L06 A02 Class Util Example Part 2
You can try experimenting with the output from functions defined within util.h
in the notebook below.
#include <string>
#include <fstream>
// Classic helper functions
class Util {
public:
static std::string convertToTime ( long int input_seconds );
static std::string getProgressBar(std::string percent);
static std::ifstream getStream(std::string path);
};
std::string Util::convertToTime (long int input_seconds)
{
long minutes = input_seconds / 60;
long hours = minutes / 60;
long seconds = int(input_seconds%60);
minutes = int(minutes%60);
std::string result = std::to_string(hours) + ":" + std::to_string(minutes) + ":" + std::to_string(seconds);
return result;
}
// constructing string for given percentage
// 50 bars is uniformly streched 0 - 100 %
// meaning: every 2% is one bar(|)
std::string Util::getProgressBar(std::string percent)
{
std::string result = "0% ";
int _size= 50;
int boundaries = (stof(percent)/100)*_size;
for (int i=0;i<_size;i++) {
if (i<=boundaries) {
result +="|";
}
else {
result +=" ";
}
}
result +=" " + percent.substr(0,5) + " /100%";
return result;
}
// wrapper for creating streams
std::ifstream Util::getStream(std::string path)
{
std::ifstream stream(path);
if (!stream) {
throw std::runtime_error("Non - existing PID");
}
return stream;
}
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: jupyter
- Opened files (when workspace is loaded): n/a